//////////////////// Mortgage market estimation ////////////////////////////////
use ${analysis}demand_estimation,clear
keep if LoanPurpose==1

gen scaler = msa_tot_raw/msa_tot_count
gen market_size = (pop*0.6/20) // 70% homeownership rate, 2 people per households, 10% look for a new home

ren sb_count sb1_count
foreach x in "sb0" "sb1" "mb" "wb"{
replace `x'_count = `x'_count*scaler 
gen `x'_share = `x'_count/market_size
gen `x'_ind_share = `x'_share/N_`x'

}
gen outside_share = outside/market_size
replace outside_share = . if outside_share<0

gmm ( ln(sb1_ind_share)-ln(outside_share)+1.14*rate_sb1 - {qs})
gmm ( ln(wb_ind_share)-ln(outside_share)+1.14*rate_wb - {qb})
gmm (rho0-{psiw0})
gmm ( rate_sb1-1/(1.14*(1-sb1_ind_share))-({eta}*rho1+{kappa}))( rate_sb0-1/(1.14*(1-sb0_ind_share))-({eta}*rho0+{kappa})),winitial(identity)

gen sbw = ( N_sb1/(N_sb1+N_sb0))/N_wb
gen sow = N_sb0/(N_sb1+N_sb0)

export delimited using "input.csv", replace 

gen qs = ln(sb1_ind_share)-ln(outside_share)+1.14*rate_sb1 
gen qb = ln(wb_ind_share)-ln(outside_share)+1.14*rate_wb
save estimate_msa,replace 

use ${analysis}demand_estimation_oil,clear

gen scaler = msa_tot_raw/msa_tot_count
gen market_size = (pop*0.6/20) // 60% homeownership rate, 2 people per households, 10% look for a new home

ren sb_count sb1_count
foreach x in "unex" "expo"{
replace `x'_count = `x'_count*scaler 
gen `x'_share = `x'_count/market_size
gen `x'_ind_share = `x'_share/N_`x'

}
gen outside_share = outside/market_size
replace outside_share = . if outside_share<0

gen sw_expo = (N_expo/(N_sb1+N_sb0))/N_shock_b
gen sw_unex = (N_unex/(N_sb1+N_sb0))/N_unshock_b

gen pre = (Year<2014)
replace pre = . if Year==2014
gen y = (ln(sw_expo)-ln(sw_unex))
gen x = (ln(expo_share/(1-expo_share))-ln(unex_share/(1-unex_share)))

gmm ({sigma}-y/x) if pre==0 

keep if Year>2011 & Year<2017
collapse y x, by(MSA pre)
sort MSA pre
by MSA: gen dy = y[1]-y[2]
by MSA: gen dx = x[1]-x[2]

reg dy dx, nocons

///////////////what drives changes in qs ?
use ${analysis}estimate_msa_local_demo,clear 

gen average_age = 25*share_20s+35*share_30s+45*share_40s+55*share_50s+65*share_60s+75*share_70s
gen ln_pop = ln(total_pop_edu)
gen ln_income = ln(per_capita_income)
gen ln_age = ln(average_age)
replace sb_share = sb_share*100

foreach x of var ln_pop ln_income ln_age share_immigrant share_minor share_college{
egen sd = sd(`x')
replace `x'=`x'/sd 
drop sd 
}

gen dq = qs-qb 
eststo clear 
eststo: areg dq ln_pop ln_income   , a(Year )
eststo: areg dq ln_pop ln_age , a(Year )
eststo: areg dq ln_pop share_immigrant , a(Year ) 
eststo: areg dq ln_pop share_minor , a(Year )
eststo: areg dq ln_pop share_college , a(Year ) 
eststo: areg dq ln_pop ln_income ln_age share_immigrant share_minor share_college  , a(Year) 

//eststo: areg sb_share ln_pop gini per_capita_income share_immigrant share_internet share_minor share_college share_no_english share_young share_below_pov employment_rate share_owner_occ share_mort if Year==2015, a(State) cluster(State)
estout, style(tex) drop(_cons)  cells(b(star fmt(2)) se(par fmt(3))) starlevels(* 0.10 ** 0.05 *** 0.010) stats(r2 r2_a N, labels(R-squared "Adjusted R-squared" "N. of Observations")) label 


